package com.jcrj.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.ftp.FTPClient;

public class FtpUploadUtil {
	private FTPClient ftpClient;
	private String url = SysConfig.getInstance().getProperty("ftp.url");

	private String userId = SysConfig.getInstance().getProperty("ftp.user");
	private String userPass = SysConfig.getInstance().getProperty("ftp.password");

	private String port = SysConfig.getInstance().getProperty("ftp.port");

	public FtpUploadUtil() {
		try {
			this.ftpClient = new FTPClient();
			this.ftpClient.connect(this.url);
			this.ftpClient.login(this.userId, this.userPass);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String uploadFile(File file, String fileName, String account) {
		String path = account + "/";
		FileInputStream in = null;
		String newFile = "";
		String filePath = null;
		try {
			String name = fileName.substring(fileName.lastIndexOf("."), fileName.length());
			newFile = new Date().getTime() + name;
			CreateDirecroty(path, this.ftpClient);
			in = new FileInputStream(file);
			this.ftpClient.setBufferSize(1024);
			this.ftpClient.setControlEncoding("UTF-8");
			this.ftpClient.enterLocalPassiveMode();
			this.ftpClient.setFileType(2);
			this.ftpClient.storeFile(newFile, in);
			filePath = path + newFile;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				if (in != null)
					in.close();
				if (this.ftpClient != null)
					this.ftpClient.disconnect();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (in != null)
					in.close();
				if (this.ftpClient != null)
					this.ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return filePath;
	}

	public void uploadMailFile(InputStream in, String path, String newFile) {
		try {
			CreateDirecroty(path, this.ftpClient);
			this.ftpClient.setBufferSize(1024);
			this.ftpClient.setControlEncoding("UTF-8");
			this.ftpClient.enterLocalPassiveMode();
			this.ftpClient.setFileType(2);
			this.ftpClient.storeFile(newFile, in);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				if (this.ftpClient != null)
					this.ftpClient.disconnect();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (this.ftpClient != null)
					this.ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void CreateDirecroty(String dirpath, FTPClient ftpClient) throws IOException {
		String directory = dirpath.substring(0, dirpath.lastIndexOf("/") + 1);
		if ((!directory.equalsIgnoreCase("/")) && (!ftpClient.changeWorkingDirectory(new String(directory)))) {
			int start = 0;
			int end = 0;
			if (directory.startsWith("/"))
				start = 1;
			else {
				start = 0;
			}
			end = directory.indexOf("/", start);
			do {
				String subDirectory = new String(dirpath.substring(start, end));
				if (!ftpClient.changeWorkingDirectory(subDirectory)) {
					if (ftpClient.makeDirectory(subDirectory))
						ftpClient.changeWorkingDirectory(subDirectory);
					else {
						System.out.println("创建目录失败");
					}
				}

				start = end + 1;
				end = directory.indexOf("/", start);
			} while (end > start);
		}
	}

	public void downLoadFile(HttpServletResponse response, String filePath, String fileName) {
		InputStream inStream = null;
		try {
			inStream = this.ftpClient.retrieveFileStream(filePath);
			String file = URLEncoder.encode(fileName, "UTF-8");
			if (file.length() > 150) {
				file = new String(fileName.getBytes("gb2312"), "ISO8859-1");
			}
			response.setCharacterEncoding("UTF-8");
			response.reset();
			response.setContentType("bin");
			response.addHeader("Content-Disposition", "attachment; filename=\"" + file + "\"");

			byte[] b = new byte[1024];
			int data;
			while ((data = inStream.read(b)) > 0) {
//				int data;
				response.getOutputStream().write(b, 0, data);
			}
		} catch (Exception localException) {
			try {
				if (inStream != null)
					inStream.close();
				if (this.ftpClient != null)
					this.ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} finally {
			try {
				if (inStream != null)
					inStream.close();
				if (this.ftpClient != null)
					this.ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public InputStream downLoadMailFile(String filePath) {
		InputStream inStream = null;
		try {
			inStream = this.ftpClient.retrieveFileStream(filePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return inStream;
	}
}

/*
 * Location: /Volumes/work/oa/WEB-INF/classes/ Qualified Name:
 * com.jcrj.util.FtpUploadUtil JD-Core Version: 0.6.0
 */